--################################################################
-- Insert COPSEMO Table
--################################################################
CREATE FUNCTION Insert_COPSEMO( INTEGER ) RETURNS void AS $$
DECLARE
	component_id ALIAS FOR $1;
BEGIN
INSERT INTO copsemo_distribution_table

VALUES (
    component_id,	--set component
	ARRAY[6.0,24.0,76.0,12.0],	--effort
	ARRAY[12.5,37.5,62.5,12.5]	--schedule
	);

END;
$$ LANGUAGE plpgsql;

--################################################################
-- Insert New Default Subsystem (Compoenents)
--################################################################
CREATE FUNCTION Insert_System(VARCHAR(30)) RETURNS INTEGER AS
'
INSERT INTO cocomo_systems_table (system_name) VALUES (
	$1 -- system_name
	);

	SELECT system_id from cocomo_systems_table order by system_id desc limit 1;
'
LANGUAGE SQL;

--################################################################
-- Insert New Default Subsystem (Compoenents)
--################################################################
CREATE FUNCTION Insert_Subsystem(VARCHAR(30), INTEGER) RETURNS INTEGER AS
'
INSERT INTO cocomo_subsystems_table
	(
		subsystem_name,
		system_id
	)

VALUES (
	$1, -- subsystem_name
	$2 -- system_id
	);

	SELECT subsystem_id from cocomo_subsystems_table order by subsystem_id desc limit 1;

'
LANGUAGE SQL;

--################################################################
-- Insert New Default Projects (Compoenents)
--################################################################
CREATE FUNCTION Insert_Component(VARCHAR(30), INTEGER) RETURNS INTEGER AS $$
DECLARE
	component_name	 ALIAS FOR $1;
	subsys_id 	 	 ALIAS FOR $2;
BEGIN
INSERT INTO cocomo_projects_table
	(
		project_name,
		subsystem_id,
		parameters_settings,
		maintenance,
		schedule_button,
		scale_factor_button,
		development_model
	)
VALUES (
	component_name, -- project_name
	subsys_id, -- subsystem_id
	--parameters_settings
	ROW(
		ROW(ARRAY[	--post_architecture
			--product
			[  0.82, 0.92, 1.00, 1.10, 1.26, 1.26 ],--RELY
			[  0.90, 0.90, 1.00, 1.14, 1.28, 1.28 ],--DATA_
			[  0.81, 0.91, 1.00, 1.11, 1.23, 1.23 ],--DOCU
			[  0.73, 0.87, 1.00, 1.17, 1.34, 1.74 ],--CPLX
			[  0.95, 0.95, 1.00, 1.07, 1.15, 1.24 ],--RUSE
			--platform
			[ 1.00, 1.00, 1.00, 1.11, 1.29,  1.63 ],--TIME_
			[ 1.00, 1.00, 1.00, 1.05, 1.17,  1.46 ],--STOR
			[ 0.87, 0.87, 1.00, 1.15, 1.30,  1.30 ],--PVOL

			--personnel
			[ 1.42, 1.19, 1.00, 0.85, 0.71, 0.71 ],--ACAP
			[ 1.22, 1.10, 1.00, 0.88, 0.81, 0.81 ],--APEX
			[ 1.34, 1.15, 1.00, 0.88, 0.76, 0.76 ],--PCAP
			[ 1.19, 1.09, 1.00, 0.91, 0.85, 0.85 ],--PLEX
			[ 1.20, 1.09, 1.00, 0.91, 0.84, 0.84 ],--LTEX
			[ 1.29, 1.12, 1.00, 0.90, 0.81, 0.81 ],--PCON

			--project
			[ 1.17, 1.09, 1.00, 0.90, 0.78, 0.78 ],--TOOL
			[ 1.22, 1.09, 1.00, 0.93, 0.86, 0.80 ],--SITE

			--user_defined_EAF
			[ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 ],--USR1
			[ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 ],--USR2

			--	schedule
			[ 1.43, 1.14, 1.00, 1.00, 1.00, 1.00 ]--SCED
		]),
		ROW(ARRAY[	--early_design
			[ 0.49, 0.60, 0.83, 1.00, 1.33, 1.91, 2.72 ],--RCPX
			[ 0.95, 0.95, 0.95, 1.00, 1.07, 1.15, 1.24 ],--RUSE
			[ 0.87, 0.87, 0.87, 1.00, 1.29, 1.81, 2.61 ],--PDIF
			[ 2.12, 1.62, 1.26, 1.00, 0.83, 0.63, 0.50 ],--PERS
			[ 1.59, 1.33, 1.12, 1.00, 0.87, 0.74, 0.62 ],--PREX
			[ 1.43, 1.30, 1.10, 1.00, 0.87, 0.73, 0.62 ],--FCIL
			[ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 ],--USR1
			[ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 ],--USR2
			[ 1.43, 1.43, 1.14, 1.00, 1.00, 1.00, 1.00 ] --SCED
		]),
		ROW(ARRAY[	--scale_factors
			[ 6.20, 4.96, 3.72, 2.48, 1.24, 0.00 ],--PREC
			[ 5.07, 4.05, 3.04, 2.03, 1.01, 0.00 ],--FLEX
			[ 7.07, 5.65, 4.24, 2.83, 1.41, 0.00 ],--RESL
			[ 5.48, 4.38, 3.29, 2.19, 1.10, 0.00 ],--TEAM
			[ 7.80, 6.24, 4.68, 3.12, 1.56, 0.00 ] --PMAT
		]),
		ROW(	--equation
			0.91,	--	B
			2.94,	--	PM
			3.67,	--	TDEV1
			0.28	--	TDEV2
		),
		ROW(	--person_month
			152.00	--hours_per_month
		),
		ROW(	--function_points_weights
			ROW( 7, 10, 15 ),--internal_logical_files
			ROW( 5, 7, 10 ),--external_interface_files
			ROW( 3, 4, 6 ),--external_inputs
			ROW( 4, 5, 7 ),--external_outputs
			ROW( 3, 4, 6 ) --external_inquiries
		)
	),
	--maintenance
	ROW(
		ROW( -- project_maintenance_typ
			1,	--precedentedness
			1,  --dev_flexibility
			1,  --risk_resolution
			1,  --team_cohesion
			1	--process_maturity
	)),
	ROW( --Schedule Button
			ROW( 4, 0 ),--ED
			ROW( 3, 0 ) --PA
	),
	ROW( -- Scale Factor Button
			ARRAY[3,3,3,3,3],--Base
			ARRAY[0,0,0,0,0] --Inc%
	)   ,
	1 -- development_model
	);

	PERFORM Insert_COPSEMO( (select project_id
							from cocomo_projects_table
							order by project_id desc limit 1) );

	RETURN project_id from cocomo_projects_table order by project_id desc limit 1;


END;
$$ LANGUAGE plpgsql;


--################################################################
-- Insert New Default Modules (SubComponents)
--################################################################
CREATE FUNCTION Insert_SubComponent(INTEGER, VARCHAR(30),VARCHAR(30)) RETURNS INTEGER AS
'
INSERT INTO sub_components_table
	(
		project_id,
		module_name,
		module_size,
		labor_rate,
		eaf
	)
 VALUES (
    $1, --project_id
    $2,	--module_name
    ROW( -- module_size
		0.0,	--breakage
		0,		--sizing_typ
		$3,	--module_language
		0,	--sloc
		ROW( -- function_points
			0, 	--change_multiplier
			ROW( 0, 0, 0 ),--internal_logical_files
			ROW( 0, 0, 0 ),--external_interface_files
			ROW( 0, 0, 0 ),--external_inputs
			ROW( 0, 0, 0 ),--external_outputs
			ROW( 0, 0, 0 )--external_inquiries
		),
		ROW( -- adaptation_and_reuse
			0.0,--initial_sloc
			100.0,--design_modified
			100.0,--code_modified
			100.0,--integration_modified
			30.0,--software_understanding
			4.0,--assement_and_assimilation
			0.4,--unfamiliarity_with_software
			0.0,--automatically_translated
			2400.0 --auto_translation_productivity
		)
	),
    0.0,	--labor_rate
    ROW( --eaf
		ROW( --early_design
			ARRAY[4,4,4,4,4,4,4,4],--Base Nom
			ARRAY[0,0,0,0,0,0,0,0] --Inc% 0%
		),
		ROW( --post_architecture
			ARRAY[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],--Base Nom
			ARRAY[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] --Inc% 0%
		)
	)
	);

	SELECT module_id from sub_components_table order by module_id desc limit 1;

'
LANGUAGE SQL;



--################################################################
-- Insert New Default Subcomponent EAF
--################################################################
CREATE FUNCTION Insert_Default_User_Def_EAF() RETURNS INTEGER AS $$
BEGIN
INSERT INTO user_def_eaf_table
	(
	user_def_name,
	eaf
	)
VALUES (
    'DEFAULT',	--set_name
	ROW( --eaf
		ROW( --early_design
			ARRAY[4,4,4,4,4,4,4,4],--Base Nom
			ARRAY[0,0,0,0,0,0,0,0] --Inc% 0%
		),
		ROW( --post_architecture
			ARRAY[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],--Base Nom
			ARRAY[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] --Inc% 0%
		)
	)
	);

	RETURN user_def_id from user_def_eaf_table order by user_def_id desc limit 1;

END;
$$ LANGUAGE plpgsql;

--################################################################
-- Insert New Default Component EAF
--################################################################
CREATE FUNCTION Insert_Default_User_Def_Parameters() RETURNS INTEGER AS $$
BEGIN

INSERT INTO user_def_pa_params_table
	(
		user_def_name,
		pa_params
	)
VALUES (
    'DEFAULT',	--set_name
	ROW(ARRAY[	--post_architecture
			--product
			[  0.82, 0.92, 1.00, 1.10, 1.26, -99.9 ],--RELY
			[ -99.9, 0.90, 1.00, 1.14, 1.28, -99.9 ],--DATA_
			[  0.81, 0.91, 1.00, 1.11, 1.23, -99.9 ],--DOCU
			[  0.73, 0.87, 1.00, 1.17, 1.34,  1.74 ],--CPLX
			[ -99.9, 0.95, 1.00, 1.07, 1.15,  1.24 ],--RUSE
			--platform
			[ -99.9, -99.9, 1.00, 1.11, 1.29,  1.63 ],--TIME_
			[ -99.9, -99.9, 1.00, 1.05, 1.17,  1.46 ],--STOR
			[ -99.9,  0.87, 1.00, 1.15, 1.30, -99.9 ],--PVOL

			--personnel
			[ 1.42, 1.19, 1.00, 0.85, 0.71, -99.9 ],--ACAP
			[ 1.22, 1.10, 1.00, 0.88, 0.81, -99.9 ],--APEX
			[ 1.34, 1.15, 1.00, 0.88, 0.76, -99.9 ],--PCAP
			[ 1.19, 1.09, 1.00, 0.91, 0.85, -99.9 ],--PLEX
			[ 1.20, 1.09, 1.00, 0.91, 0.84, -99.9 ],--LTEX
			[ 1.29, 1.12, 1.00, 0.90, 0.81, -99.9 ],--PCON

			--project
			[ 1.17, 1.09, 1.00, 0.90, 0.78, -99.9 ],--TOOL
			[ 1.22, 1.09, 1.00, 0.93, 0.86, 0.80 ],--SITE

			--user_defined_EAF
			[ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 ],--USR1
			[ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 ],--USR2

			--	schedule
			[ 1.43, 1.14, 1.00, 1.00, 1.00, -99.9 ]--SCED
		])
	);

	RETURN user_def_id from user_def_pa_params_table order by user_def_id desc limit 1;
END;
$$ LANGUAGE plpgsql;

--################################################################
-- Insert New Default Component Scale Factors
--################################################################
CREATE FUNCTION Insert_Default_User_Def_SF(  ) RETURNS INTEGER AS $$
BEGIN
INSERT INTO user_def_scale_factor_table
	(
		user_def_name,
		scale_factors
	)
VALUES (
    'DEFAULT',	--set_name
	ROW(ARRAY[	--scale_factors
			[ 6.20, 4.96, 3.72, 2.48, 1.24, 0.00 ],--PREC
			[ 5.07, 4.05, 3.04, 2.03, 1.01, 0.00 ],--FLEX
			[ 7.07, 5.65, 4.24, 2.83, 1.41, 0.00 ],--RESL
			[ 5.48, 4.38, 3.29, 2.19, 1.10, 0.00 ],--TEAM
			[ 7.80, 6.24, 4.68, 3.12, 1.56, 0.00 ] --PMAT
	])
	);

	RETURN user_def_id from user_def_scale_factor_table order by user_def_id desc limit 1;
END;
$$ LANGUAGE plpgsql;

--################################################################
-- Insert New Default Component Scale Factors Button
--################################################################
CREATE FUNCTION Insert_Default_User_Def_SF_Button(  ) RETURNS INTEGER AS $$
BEGIN
INSERT INTO user_def_sf_button_table
	(
		user_def_name,
		scale_factors
	)
VALUES (
    'DEFAULT',	--set_name
	ROW( -- Scale Factor Button
			ARRAY[3,3,3,3,3],--Base
			ARRAY[0,0,0,0,0] --Inc%
	)
	);
	RETURN user_def_id from user_def_sf_button_table order by user_def_id desc limit 1;
END;
$$ LANGUAGE plpgsql;

--################################################################
-- Insert New Default Component Equation
--################################################################
CREATE FUNCTION Insert_Default_User_Def_Equation( ) RETURNS INTEGER AS $$
BEGIN

INSERT INTO user_def_equation_table
	(
		user_def_name,
		equation
	)
VALUES (
    'DEFAULT',	--set_name
	ROW(	--equation
		0.91,	--	B
		2.94,	--	PM
		3.67,	--	TDEV1
		0.28	--	TDEV2
	)
	);
	RETURN user_def_id from user_def_equation_table order by user_def_id desc limit 1;
END;
$$ LANGUAGE plpgsql;

--################################################################
-- Insert New Default Component Function Points
--################################################################
CREATE FUNCTION Insert_Default_User_Def_Function_Point(  ) RETURNS INTEGER AS $$
BEGIN

INSERT INTO user_def_function_points_table
	(
		user_def_name,
		function_points
	)
VALUES (
    'DEFAULT',--set_name
	ROW( -- function_points
		0, 	--change_multiplier
		ROW( 0, 0, 0 ),--internal_logical_files
		ROW( 0, 0, 0 ),--external_interface_files
		ROW( 0, 0, 0 ),--external_inputs
		ROW( 0, 0, 0 ),--external_outputs
		ROW( 0, 0, 0 )--external_inquiries
	)
	);
	RETURN user_def_id from user_def_function_points_table order by user_def_id desc limit 1;
END;
$$ LANGUAGE plpgsql;

--################################################################
-- Insert New Default Component Function Points Weights
--################################################################
CREATE FUNCTION Insert_Default_User_Def_FP_Weights( ) RETURNS INTEGER AS $$
BEGIN

INSERT INTO user_def_function_points_weight_table
	(
		user_def_name,
		fp_weights
	)
VALUES (
    'DEFAULT',	--set_name
	ROW(	--function_points_weights
		ROW( 7, 10, 15 ),--internal_logical_files
		ROW( 5, 7, 10 ),--external_interface_files
		ROW( 3, 4, 6 ),--external_inputs
		ROW( 4, 5, 7 ),--external_outputs
		ROW( 3, 4, 6 ) --external_inquiries
	)
	);
	RETURN user_def_id from user_def_function_points_weight_table order by user_def_id desc limit 1;
END;
$$ LANGUAGE plpgsql;


--Insert Default values into User Defined tables
SELECT Insert_Default_User_Def_Parameters();
SELECT Insert_Default_User_Def_SF();
SELECT Insert_Default_User_Def_SF_Button();
SELECT Insert_Default_User_Def_EAF();
SELECT Insert_Default_User_Def_Equation();
SELECT Insert_Default_User_Def_Function_Point();
SELECT Insert_Default_User_Def_FP_Weights();


